package org.example.netty.test.nio.bean;

import lombok.Data;
import lombok.ToString;

import java.io.*;

/**
 * @author zp
 * @version 1.0
 * @description: 登入的消息体
 * @date 2023/3/30 19:54
 */
@ToString
public class LogMessage extends Message {
    private String userName;
    private String password;

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public LogMessage(String userName, String password) {
        this.userName = userName;
        this.password = password;
    }

    @Override
    public byte[] messageType() {
        return LOGIN_TYPE;
    }

    @Override
    public byte[] messageTypeStatus() {
        return new byte[]{0};
    }

    @Override
    public LogMessage messageByteArrayDecode(byte[] data) {

        ByteArrayInputStream bytes = new ByteArrayInputStream(data);
        ObjectInputStream in = null;
        try {
            in = new ObjectInputStream(bytes);
            return (LogMessage) in.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public byte[] messageByteArrayEncode(Message message) {
        ByteArrayOutputStream bas = new ByteArrayOutputStream();
        try {
            ObjectOutputStream out = new ObjectOutputStream(bas);
            out.writeObject(message);
            return bas.toByteArray();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
